//
//  FootViewController.m
//  项目A --半糖
//
//  Created by romana on 16/5/28.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "FootViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "TitleModel.h"
#import "BoottonTableViewCell.h"
#import "FootDeWebViewController.h"

@interface FootViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation FootViewController


-(void)viewWillAppear:(BOOL)animated{
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    if (self.titleStr) {
        titleLabel.text = self.titleStr;
    }else{
        titleLabel.text = @"攻略详情";
    }
    
    
    self.navigationItem.titleView = titleLabel;
    
    // tableView的建立
    [self createTableView];
    // 数据解析
    //    [self dataHandel];
    self.dataArr = [NSMutableArray array];
    [self getData];
    
    // Do any additional setup after loading the view.
}

-(void)createTableView{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:tableView];
    [tableView registerClass:[BoottonTableViewCell class] forCellReuseIdentifier:@"tableViewCell"];
    self.tableView = tableView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BoottonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell" forIndexPath:indexPath];
    TitleModel *t = self.dataArr[indexPath.row];
    cell.imgView.layer.cornerRadius = 10;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:t.cover_image_url]];
    cell.txtLable.text = t.title;
    cell.webUrl = t.url;
    tableView.rowHeight = self.view.frame.size.height / 4;
    return cell;
}

-(void)getData{
  NSString *str = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?limit=20&gender=2&offset=0&generation=2&order_by=now",_detailId];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dataDic = responseObject[@"data"];
        NSArray *arr = dataDic[@"items"];
        for (NSDictionary *itemsDic in arr) {
            TitleModel *t = [[TitleModel alloc] init];
            [t setValuesForKeysWithDictionary:itemsDic];
            [self.dataArr addObject:t];
        }
        [self.tableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];





}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    FootDeWebViewController *footDWVC = [[FootDeWebViewController alloc] init];
    //    footDWVC.index = indexPath.row;
    // 传id
    TitleModel *t = self.dataArr[indexPath.row];
    footDWVC.footId = t.myId;
    [self.navigationController pushViewController:footDWVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
